//
//  EmoticonAttachment.swift
//  表情键盘
//
//  Created by wxy on 15/9/18.
//  Copyright © 2015年 wxy. All rights reserved.
//

import UIKit

class EmoticonAttachment: NSTextAttachment {
    
    // 表情文字
    var chs: String
    
    init(chs: String) {
        self.chs = chs
        
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 创建表情属性文本
    class func emoticonAttributeText(emoticon: Emoticon, font: UIFont) -> NSAttributedString {
        
        let attachment = EmoticonAttachment(chs: emoticon.chs!)
        
        attachment.image = UIImage(contentsOfFile: emoticon.imagePath)
        
        // 图片高度
        let height = font.lineHeight
        // bounds 的 x/y 就是 scrollView 的contentOffset 平股票利用 bounds 的 x/y 能调整控件内部的偏移位置
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        // 创建图片属性字符串
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        // 添加 图像字体 保证图片表情与字体等大小
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText

    }
}
